package cn.com.ghy.android.widget.report;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.OrderDetailBean;
import cn.com.ghy.android.bean.OrderReportInfoBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.OrderOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;
import cn.com.ghy.android.widget.chat.OrderInfo;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单报表列表显示
 * @author wuz  @date 2011-5-20 08:33:00
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderReportList extends Activity {

	private ListView listView = null;
	private TextView labTxtOrderReportNum = null;
	private TextView labTxtOrderReportTotal = null;
	private ProgressDialog progressDialog = null;

	private Intent intent = null;
	private Bundle bundle = null;

	private List<OrderReportInfoBean> orderReportInfoList = null;
	private OrderReportListAdapter orderReportListAdapter = null;

	private HttpThread thread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.order_report_list);
		getSession();
		findView();
		init();
		addListener();
	}

	private void findView() {
		listView = (ListView) this.findViewById(R.id.listOrderReportList);
		labTxtOrderReportNum = (TextView) this.findViewById(R.id.labTxtOrderReportNum);
		labTxtOrderReportTotal = (TextView) this.findViewById(R.id.labTxtOrderReportTotal);
	}

	private void init() {
		labTxtOrderReportNum.setText("0");
		labTxtOrderReportTotal.setText("0.00");

		orderReportListAdapter = new OrderReportListAdapter(OrderReportList.this);
		listView.setAdapter(orderReportListAdapter);

		if (null != orderReportInfoList && orderReportInfoList.size() > 0) {
			for (int i = 0; i < orderReportInfoList.size(); i++) {
				orderReportListAdapter.add(orderReportInfoList.get(i));
			}
			calculateTotalCount(orderReportInfoList);
		}
	}

	@SuppressWarnings("unchecked")
	public void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		orderReportInfoList = (List<OrderReportInfoBean>) this.getIntent().getSerializableExtra(
				"orderReportInfolist");
	}

	private void addListener() {
		listView.setOnItemClickListener(listViewListener);
	}

	private void calculateTotalCount(List<OrderReportInfoBean> list) {
		int orderReportNum = 0;
		double orderReportTotal = 0.00;

		for (int i = 0; i < list.size(); i++) {
			orderReportTotal += Double.parseDouble(list.get(i).getTotalmoney());
		}
		orderReportNum = orderReportInfoList.size();
		labTxtOrderReportNum.setText(orderReportNum + "");
		java.text.DecimalFormat df = new java.text.DecimalFormat("######0.00");
		labTxtOrderReportTotal.setText(df.format(orderReportTotal));
	}

	private class OrderReportListAdapter extends BaseAdapter {

		private Context context = null;
		private List<OrderReportInfoBean> list = new ArrayList<OrderReportInfoBean>();
		private OrderReportInfoBean bean = null;

		public OrderReportListAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return list.size();
		}

		public OrderReportInfoBean getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
					R.layout.order_report_list_lay, null);
			if (list.get(position) instanceof OrderReportInfoBean) {
				bean = list.get(position);
				TextView tv = (TextView) linearLayout.findViewById(R.id.labTxtAdviceOrderId);
				tv.setText(bean.getOrderid());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtHandOrderId);
				tv.setText(bean.getOrderno());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtCustname);
				tv.setText(bean.getCustname());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtOrderStatus);
				tv.setText(bean.getIspassname());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtOrderTotalNum);
				tv.setText(bean.getTotalnum());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtOrderTotalMoney);
				tv.setText(bean.getTotalmoney() + "元");
				tv = (TextView) linearLayout.findViewById(R.id.labTxtSendGoodsStatus);
				tv.setText(bean.getIssendname());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtOrderTotalSendNum);
				tv.setText(bean.getTotalsendnum());
				tv = (TextView) linearLayout.findViewById(R.id.labTxtOrderTotalSendMoney);
				tv.setText(bean.getTotalsendmoney() + "元");

			}
			return linearLayout;
		}

		public void add(OrderReportInfoBean rderReportInfoBean) {
			list.add(rderReportInfoBean);
			notifyDataSetChanged();
		}
	}

	private OnItemClickListener listViewListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			showDialog(OrderReportList.this, position,
					((OrderReportListAdapter) parent.getAdapter()).getItem(position));
		}
	};

	private void showDialog(final Context context, final int position,
			final OrderReportInfoBean bean) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示信息");
		builder.setMessage("订单编号 " + bean.getOrderid());
		builder.setPositiveButton("查看", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String intentflag = "look";
				thread = new HttpThread(handler);
				thread.doStart(bean.getOrderid(), intentflag);
			}
		});
		if ("W".equals(bean.getIspass()) && !"已取消".equals(bean.getOrderzt())) {
			builder.setNeutralButton("修改", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					String intentflag = "modify";
					thread = new HttpThread(handler);
					thread.doStart(bean.getOrderid(), intentflag);
				}
			});
		}

		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		builder.show();
	}

	private class HttpThread extends Thread {
		private Handler handle = null;
		private String orderid;
		private String intentflag = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart(String orderid, String intentflag) {
			this.orderid = orderid;
			this.intentflag = intentflag;
			progressDialog = new ProgressDialog(OrderReportList.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				Map<String, Object> map = null;
				OrderOpeService orderOpeService = new OrderOpeService();
				map = orderOpeService.OrderShow(orderid);
				if (null != map && map.size() > 0) {
					progressDialog.dismiss();
					message = handle.obtainMessage();
					bundle = new Bundle();
					message.what = CommonStatic.STATE_NORMAL;
					bundle.putSerializable("map", (Serializable) map);
					bundle.putString("intentflag", intentflag);
					message.setData(bundle);
					handle.sendMessage(message);
				} else {
					progressDialog.dismiss();
					message = handle.obtainMessage();
					message.what = CommonStatic.STATE_NORMAL_NULL;
					handle.sendMessage(message);
				}
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					Map<String, Object> map = (Map<String, Object>) m.getData().getSerializable(
							"map");
					String intentflag = (String) m.getData().getString("intentflag");
					CustomerInfoBean customerInfoBean = (CustomerInfoBean) map
							.get("customerInfoBean");
					List<OrderDetailBean> orderDetailList = (List<OrderDetailBean>) map
							.get("orderDetailList");
					bundle = new Bundle();
					bundle.putSerializable("customerInfo", customerInfoBean);
					bundle.putSerializable("listOrderDetail", (Serializable) orderDetailList);
					bundle.putString("intentflag", intentflag);
					if ("look".equals(intentflag)) {
						intent = new Intent(OrderReportList.this, OrderInfoView.class);
					} else if ("modify".equals(intentflag)) {
						intent = new Intent(OrderReportList.this, OrderInfo.class);
					}
					intent.putExtras(bundle);
					OrderReportList.this.startActivity(intent);
					break;
				case CommonStatic.STATE_NORMAL_NULL :
					new AlertDialog.Builder(OrderReportList.this).setTitle("提示信息")
							.setMessage("该条件,暂时没有订单信息!").setNeutralButton("关闭", null).show();
					break;
			}
		}
	};
}
